/**
* @file LocalPositionSimulator.cpp in SimulateLPS
* @author rebeater
* @comment
* Create on 3/22/22 10:58 PM
* @version 1.0
**/


#include "LocalPositionSimulator.h"
#include "LpsData.h"
#include "glog/logging.h"
#include <QSerialPortInfo>
LocalPositionSimulator::LocalPositionSimulator(bool repeat) : repeat{repeat}, Simulator() {
  data.clear();
  auto serials = QSerialPortInfo::availablePorts();
  for (auto &s:serials) {
    if (s.hasVendorIdentifier() and s.vendorIdentifier() == 0x067b) {
	  SetSerialPortInfo(s);
	  LOG(INFO) <<"select "<<s.portName().toStdString();
	  break;
	}
  }
}
bool LocalPositionSimulator::IsRunning() const {
  return repeat or running;
}
void LocalPositionSimulator::LoadFile(const std::string &file) {
  GnssReader gnss_reader{file, GnssFileFormat::GNSS_10_LINES};
  if (!gnss_reader.IsOk()) {
	running = false;
	return;
  }
  bool ok;
  do {
	GnssData gnss;
	ok = gnss_reader.ReadNext(gnss);
	data.push_back(gnss);
  } while (ok);
  running = true;
}
bool LocalPositionSimulator::LpsOutput() {
  if (index == data.size()) {
	if (repeat) {
	  index = 0;
	} else {
	  running = false;
	  return false;
	}
  }
  auto gnss = data[index++];
  auto lps = FromGnssData(gnss);
  LOG(INFO) <<"Output:"<<lps.gpst<<" "<<gnss;
  return SerialOutput(reinterpret_cast<char *>(&lps), sizeof lps);
}
